initArtCateList();
let layer = layui.layer;
let form = layui.form;
let indexAdd = null;
let indexEdit = null;
function initArtCateList() {
    $.ajax({
        method: 'get',
        url: '/my/article/cates',
        success: function (res) {
            $('.layui-bg-green').html(res.data.length)
            let htmlStr = template('tpl', res)
            $('tbody').html(htmlStr)
        }
    })
}

$('#btnAddCate').on('click', function () {
    indexAdd = layer.open({
        type: 1,
        area: ['500px', '250px'],
        title: '添加文章分类',
        content: $('#tanchu').html()
    })
})

$('body').on('submit', '#form-add', function (e) {
    e.preventDefault();
    $.ajax({
        method: 'post',
        url: '/my/article/addcates',
        data: $(this).serialize(),
        success: function (res) {
            if (res.status !== 0) {
                return layer.msg('新增分类失败')
            }
            initArtCateList();
            layer.msg('新增分类成功');
            layer.close(indexAdd)
        }
    })
})

$('tbody').on('click', '.btn-edit', function () {
    indexEdit = layer.open({
        type: 1,
        area: ['500px', '250px'],
        title: '修改文章分类',
        content: $('#tanchu-edit').html()
    })
    let id = $(this).attr('data-id');
    $.ajax({
        method: 'get',
        url: '/my/article/cates/' + id,
        success: function (res) {
            console.log(res);
            form.val('form-edit',res.data)
        }
})
})
$('body').on('submit', '#form-edit', function (e) {
    e.preventDefault();
    $.ajax({
        method: 'post',
        url: '/my/article/updatecate',
        data: $(this).serialize(),
        success: function (res) {
            if (res.status !== 0) {
                layer.msg('更新数据失败')
            }
            layer.msg('更新数据成功');
            layer.close(indexEdit)
            initArtCateList()
        }
    })
})

$('tbody').on('click', '.btn-delete', function () {
    let id = $(this).attr('data-id');
    layer.confirm('确认删除？', { icon: 3, title: '提示' }, function (index) {
        $.ajax({
            method: 'get',
            url: '/my/article/deletecate/' + id,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('删除分类失败')
                }
                layer.msg('删除分类成功');
                layer.close(index)
                initArtCateList()
            }
        })
    })
})